package org.example.obj;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;

import java.util.Arrays;

public class MqttConfiguration {
    //    MqttBrokerUrl
    private  String brokerUrl  = "tcp://10.30.206.231";
    //   MqttBrokerProt
    private  String port = "1883";
    //    clientId
    private  String clientID = "default_client";
    //      topic
    private  String topic = "default/normal";
    //  qos
    private  Integer qos = 0;
    // topic 最长长度
    private final  int MAXTOPIC  = 65535;
    // message
     private byte[] bytes;
//     username
    private String username;
//    password
    private String password;
//     option
    private MqttConnectOptions MO = new MqttConnectOptions() ;

    public MqttConfiguration(byte[] bytes, String clientID, Integer qos, String topic, String port, String brokerUrl) {
        this.bytes = bytes;
        this.clientID = clientID;
        this.qos = qos;
        this.topic = topic;
        this.port = port;
        this.brokerUrl = brokerUrl;

    }

    public MqttConfiguration(){}

//    是否失败重连 ，




    public String getBrokerUrl() {
        return brokerUrl;
    }

    public void setBrokerUrl(String brokerUrl) {
        this.brokerUrl = brokerUrl;
    }

    public Integer getQos() {
        return qos;
    }

    public void setQos(Integer qos) {
        this.qos = qos;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public String getClientID() {
        return clientID;
    }

    public void setClientID(String clientID) {
        this.clientID = clientID;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getPort() {
        return port;
    }


    public void setPort(String port) {
        this.port = port;
    }

    public MqttConnectOptions getMO() {
        return MO;
    }

    public int getMAXTOPIC() {
        return MAXTOPIC;
    }

    /**
     * 是否有账号
     * @param username 账号
     * @param password 密码
     */
    public void register(String username,String password){
        this.username = username;
        this.password = password;
        MO.setUserName(username);
        MO.setPassword(password.toCharArray());
    }

    /**
     *
     * @param clearSession  是否清除会话
     * @param keepAlive 心跳时间
     * @param timeOut 超出时间
     * @param Reconnect 是否自动重连
     */
    public void setMO(Boolean clearSession ,int keepAlive,int timeOut ,boolean Reconnect) {

        MO.setCleanSession(clearSession);
        MO.setKeepAliveInterval(keepAlive);
        MO.setConnectionTimeout(timeOut);
        MO.setAutomaticReconnect(Reconnect);
    }

    /**
     *  设置遗留消息
     * @param message 遗留消息体
     * @param retained 是否有保留消息
     */
    public void  setWill(String message,Boolean retained){
        MO.setWill(topic,message.getBytes(),qos,retained);
    }
    @Override
    public String toString() {
        return "MqttConfiguration{" +
                "brokerUrl='" + brokerUrl + '\'' +
                ", port='" + port + '\'' +
                ", clientID='" + clientID + '\'' +
                ", topic='" + topic + '\'' +
                ", qos=" + qos +
                ", bytes=" + Arrays.toString(bytes) +
                '}';
    }

}
